package com.nhc.activity_fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.astrob.nhcmap.R;
import com.astrob.view.BaseViewHolder;

import java.util.ArrayList;
import java.util.List;

import zuo.biao.library.base.BaseActivity;
import zuo.biao.library.base.BaseAdapter;
import zuo.biao.library.model.Entry;
import zuo.biao.library.ui.PageScroller;

public class SysSetActivity extends BaseActivity {
    private final String TAG = "SysSetActivity";

    //启动方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


    /**
     * 启动这个Activity的Intent
     *
     * @param context
     * @return
     */
    public static Intent createIntent(Context context) {
        return new Intent(context, SysSetActivity.class);
    }
    //启动方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sysset_activity);

        //功能归类分区方法，必须调用<<<<<<<<<<
        initView();
        initData();
        initEvent();
        //功能归类分区方法，必须调用>>>>>>>>>>
    }


    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    //示例代码<<<<<<<<
    private ListView lv_setting;

    //示例代码>>>>>>>>
    @Override
    public void initView() {//必须在onCreate方法内调用
        autoSetTitle();//自动设置标题为上个Activity传入的INTENT_TITLE

        //示例代码<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        lv_setting = (ListView) findViewById(R.id.lv_setting);
        //示例代码>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    }

    //示例代码<<<<<<<<
    private DemoAdapter adapter;
    //示例代码>>>>>>>>

    /**
     * 示例方法 ：显示列表内容
     *
     * @param list
     */
    private void setList(List<Entry<Integer, Integer>> list) {
        if (adapter == null) {
            adapter = new DemoAdapter(context);
            lv_setting.setAdapter(adapter);
        }
        adapter.refresh(list);
    }
    //UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    //示例代码<<<<<<<<

    boolean mIsscreenshot = true;//可以截图

    private List<Entry<Integer, Integer>> searchtypelist;
    //    private final int[] SETTING_IDS = {
//            R.drawable.settings_language, R.drawable.settings_satellite_system, R.drawable.settings_satellite_mode,
//            R.drawable.settings_backlight, R.drawable.settings_time, R.drawable.settings_screenshot};
//
//    private final String[] SETTING_NAMES = {
//            "语言", "卫星系统", "卫星模式",
//            "背光时间", "时间", "开启截图"};
    private final int[] SETTING_IDS = {
            R.drawable.settings_language, R.drawable.settings_satellite_system, R.drawable.settings_satellite_mode,
            R.drawable.settings_backlight, R.drawable.settings_time};

    //示例代码>>>>>>>>>
    @Override
    public void initData() {//必须在onCreate方法内调用
        //示例代码<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        showProgressDialog(R.string.loading);
        runThread(TAG + "initData", new Runnable() {
            @Override
            public void run() {
                searchtypelist = getList();
                runUiThread(new Runnable() {
                    @Override
                    public void run() {
                        dismissProgressDialog();
                        setList(searchtypelist);
                    }
                });
            }
        });
        //示例代码>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    }

    protected List<Entry<Integer, Integer>> getList() {
        String[] SETTING_NAMES = getResources().getStringArray(R.array.setting_names);
        searchtypelist = new ArrayList<Entry<Integer, Integer>>();
        for (int i = 0; i < SETTING_NAMES.length; i++) {
            int swich = 0;
//            if (i == SETTING_NAMES.length - 1) {
//                swich = 1;
//            }
            searchtypelist.add(new Entry<Integer, Integer>(i, swich));
        }
        return searchtypelist;
    }

    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Event事件区(只要存在事件监听代码就是)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    @Override
    public void initEvent() {//必须在onCreate方法内调用
        //示例代码<<<<<<<<<<<<<<<<<<<
        lv_setting.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                onToNext(position);
            }
        });

        //分页滚动示例代码<<<<<<<<<<<<<<<<<<<
        new PageScroller(lv_setting).init();
        //分页滚动示例代码>>>>>>>>>>>>>>>>>>>

        //示例代码>>>>>>>>>>>>>>>>>>>
    }

    private void onToNext(int pos) {
        switch (pos) {
            default:
                break;
            case 0:
                onLanguage();
                break;
            case 1:
                onGpsSys();
                break;
            case 2:
                onGpsSys();
                break;
            case 3:
                onShowSys();
                break;
            case 4:
                onTimeSys();
                break;
            case 5:
                mIsscreenshot = !mIsscreenshot;
                adapter.notifyDataSetChanged();
                break;
        }

    }

    void onLanguage() {
        Intent intent = new Intent(Settings.ACTION_LOCALE_SETTINGS);
        startActivity(intent);
    }

    void onGpsSys() {
        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        startActivityForResult(intent, 1);

    }

    void onTimeSys() {
        Intent intent = new Intent(Settings.ACTION_DATE_SETTINGS);
        startActivityForResult(intent, 1);
    }

    void onShowSys() {
        Intent intent = new Intent(Settings.ACTION_DISPLAY_SETTINGS);
        startActivityForResult(intent, 1);

    }


    //系统自带监听方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    //@user 新增方法

    @Override
    public void finish() {
        super.finish();
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                break;
        }
        return super.onKeyUp(keyCode, event);
    }

//    private final int REQUEST_TO_BOTTOM_MENU = 1;
//    private final int REQUEST_TO_EDIT_TEXT_INFO = 2;

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) {
            return;
        }
//        switch (requestCode) {
//            case REQUEST_TO_BOTTOM_MENU:
//                if (data != null) {
//                    onBottomMenuItemClick(data.getIntExtra(BottomMenuWindow.RESULT_ITEM_ID, -1));
//                }
//                break;
//            case REQUEST_TO_EDIT_TEXT_INFO:
//                if (user == null) {
//                    user = new User(userId);
//                }
//                user.setTag(data == null ? null : data.getStringExtra(EditTextInfoActivity.RESULT_VALUE));
//                setUser(user);
//                break;
//        }
    }


    //系统自带监听方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    //Event事件区(只要存在事件监听代码就是)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    //Adapter
    public class DemoAdapter extends BaseAdapter<Entry<Integer, Integer>> {
        public DemoAdapter(Activity context) {
            super(context);
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(context).inflate(
                        R.layout.icontext_open_view, parent, false);
            }

            TextView tv_name = BaseViewHolder.get(convertView, R.id.tv_name);
            ImageView iv_icon = BaseViewHolder.get(convertView, R.id.iv_icon);
            ImageView iv_swith = BaseViewHolder.get(convertView, R.id.iv_swith);
            ImageView iv_rt = BaseViewHolder.get(convertView, R.id.iv_rt);

            final Entry<Integer, Integer> kvb = getItem(position);
            int index = kvb.getKey();
            if (index < 0 || index >= SETTING_IDS.length) {
                index = 0;
            }
            String[] SETTING_NAMES = getResources().getStringArray(R.array.setting_names);
            tv_name.setText(SETTING_NAMES[index]);
            iv_icon.setImageResource(SETTING_IDS[index]);

            int switchmode = kvb.getValue();

            if (switchmode == 0) {
                iv_swith.setVisibility(View.GONE);
                iv_rt.setVisibility(View.VISIBLE);
            } else {
                iv_swith.setVisibility(View.VISIBLE);
                iv_rt.setVisibility(View.GONE);
                if (mIsscreenshot) {
                    iv_swith.setImageResource(R.drawable.swith_open);
                } else {
                    iv_swith.setImageResource(R.drawable.swith_close);
                }
            }

            return super.getView(position, convertView, parent);
        }

        private final long serialVersionUID = 3L;

        @Override
        public long getItemId(int position) {
//            return getItem(position).getId();
            return serialVersionUID + position;
        }
    }

}
